<template>
  <view>
    <cu-custom bgColor="bg-gradual-blue" :isBack="true">
      <block slot="backText">返回</block><block slot="content">垃圾分类版2048</block>
    </cu-custom>
    <view class="main">
      <img src='../../static/2048bg.png' class="image" mode="widthFix"/>
      <view class="container" style="height: 95vh; width: 100%;">
        <view class="game-body">
          <loading v-show="loading">
            加载中...
          </loading>
          <view class="rule">
            <text @tap="showDialog" data-target="showPlayingMethod">玩法</text>
          </view>
          <view class="flex solid-bottom padding justify-center">
            <view class="bg-green padding-sm padding-lr-lg margin-df">
              <text>得分： {{score}}</text>
              <text style="padding-left: 20px">最高分： {{highestScore}}</text>
            </view>
          </view>

          <view class="game">
            <view class="restart-button-class">
              <text class="cuIcon-refresh" @click="restart"></text>
            </view>
            <view class="game-container">
              <view class="game-message" :class="over ? (win ? 'game-won' : 'game-over') : ''">
                <text class="over-msg">{{overMsg}}</text>
                <view class="lower">
                  <text class="retry-button" @click="restart">再来一次</text>
                </view>
              </view>
              <view class="grid-container" @touchstart="touchStart" @touchmove="touchMove" @touchend="touchEnd">
                <view v-for="(rows, rowIndex) in grids" :key="'row' + rowIndex" class="grid-row">
                  <view v-for="(cell, columnIndex) in rows" :key="'column' + columnIndex" class="grid-cell">
                    <view class="tile">
                      <view v-if="cell" class="tile-inner" :class="'cell-' + cell.type">
                        <view class="tile-text">{{cell.name}}</view>
                      </view>
                    </view>
                  </view>
                </view>
              </view>
            </view>
            <view>
              <img src="../../static/rubbish.png" class="footer-img"/>
            </view>
          </view>
        </view>
      </view>
    </view>
    <view class="cu-modal" :class="showPlayingMethod ? 'show' : ''">
      <view class="cu-dialog">
        <view class="cu-bar bg-white justify-end">
          <view class="content">玩法</view>
          <view class="action" @tap="hideDialog">
            <text class="cuIcon-close text-red"></text>
          </view>
        </view>
        <view class="padding-xl">
          有16个格子，初始时会有两个格子上安放了两个垃圾方块，每次可以选择上下左右其中一个方向去滑动，每滑动一次，所有的垃圾方块都会往滑动的方向靠拢外，系统也会在空白的地方随即出现一个垃圾方块，相同数类型的方块在靠拢、相撞时会合并，分数会加2。
        </view>
        <view class="cu-bar bg-white justify-end">
          <view class="action">
            <text class="margin-left" @tap="hideDialog">我知道了</text>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
import GameManager from '../../utils/game_manager';

export default {
  name: "garbage2048",
  data () {
    return {
      loading: false,
      gameManager : {},
      grids: [],
      over: false,
      win: false,
      score: 0,
      highestScore: wx.getStorageSync('highestScore') || 0,
      overMsg: 'Game Over！',
      touchStartClientX: 0,
      touchStartClientY: 0,
      touchEndClientX: 0,
      touchEndClientY: 0,
      isMultiple: false, // 多手指操作
      showPlayingMethod: false,
    }
  },
  mounted () {
    this.loading = true;
    this.highestScore = wx.getStorageSync('highestScore') || 0;
    this.gameManager = new GameManager(4);
    this.grids = this.gameManager.setup();
    this.highestScore = 0;
    this.loading = false;
  },
  beforeUpdate () {
    if (this.highestScore === 0) {
      this.highestScore = wx.getStorageSync('highestScore') || 0;
    }
  },
  methods: {
    restart () {
      this.grids = this.gameManager.restart();
      this.over = false;
      this.won = false;
      this.score = 0;
    },
    touchStart (events) {
      // 多指操作
      this.isMultiple = events.touches.length > 1;
      if (this.isMultiple) {
        return;
      }
      let touch = events.touches[0];
      this.touchStartClientX = touch.clientX;
      this.touchStartClientY = touch.clientY;
    },

    touchMove (events) {
      let touch = events.touches[0];
      this.touchEndClientX = touch.clientX;
      this.touchEndClientY = touch.clientY;
    },

    touchEnd (events) {
      if (this.isMultiple) {
        return;
      }
      let dx = this.touchEndClientX - this.touchStartClientX;
      let absDx = Math.abs(dx);
      let dy = this.touchEndClientY - this.touchStartClientY;
      let absDy = Math.abs(dy);
      if (Math.max(absDx, absDy) > 10) {
        let direction = absDx > absDy ? (dx > 0 ? 1 : 3) : (dy > 0 ? 2 : 0);
        let data = this.gameManager.move(direction) || {
          grids: this.data.grids,
          over: this.data.over,
          won: this.data.won,
          score: this.data.score
        };
        this.grids = data.grids;
        this.over = data.over;
        this.won = data.won;
        this.score = data.score;
        this.highestScore = Math.max(this.highestScore, data.score);
        const lastHighestScore = wx.getStorageSync('highestScore') || 0;
        if (this.highestScore > lastHighestScore) {
          wx.setStorageSync('highestScore', this.highestScore);
        }
      }
    },

    showDialog () {
      this.showPlayingMethod = true;
    },

    hideDialog () {
      this.showPlayingMethod = false;
    }
  },
}
</script>

<style scoped>
.main {
  width: 100%;
  height: 100%;
  position: fixed;
  flex-direction: column;
  justify-content: space-around;
  background-color: #fff8f0;
}
.image {
  position: absolute;
  left: 0;
  top: 0;
  height: 100% !important;
  display: block;
  width: 100%;
  z-index: -999;
}
.rule {
  height: 20px;
  width: 100%;
  text-align: right;
  padding-right: 5px;
}

.container {
  margin: 0;
  padding: 5px 0 20px 0;
  background-color: rgba(255, 255, 255, 0.1);
  color: #776e65;
  font-family: "Helvetica Neue", Arial, sans-serif;
  font-size: 18px;
}

.heading:after {
  content: "";
  display: block;
  clear: both;
}
.tile-text {
  display:block;
  align-self: center;
  height: auto;
  justify-content:center;
  width: 70px;
}
.restart-button-class{
  height: 30px;
  color: #43aa8b;
  text-align: right;
  font-weight: bold;
  font-size: 30px;
  padding-right: 12%;
  padding-bottom: 20px;
}
.footer-img {
  width: 100%;
  height: 240px;
}
p {
  margin-top: 0;
  margin-bottom: 10px;
  line-height: 1.65;
}
a {
  color: #776e65;
  font-weight: bold;
  text-decoration: underline;
  cursor: pointer;
}
strong.important {
  text-transform: uppercase;
}
hr {
  border: none;
  border-bottom: 1px solid #d8d4d0;
  margin-top: 20px;
  margin-bottom: 30px;
}
.game {
  /*position: relative;*/
  /*padding: 15px;*/
  /*cursor: default;*/
  /*-webkit-touch-callout: none;*/
  /*-ms-touch-callout: none;*/
  /*-webkit-user-select: none;*/
  /*-moz-user-select: none;*/
  /*-ms-user-select: none;*/
  /*-ms-touch-action: none;*/
  /*touch-action: none;*/
  /*width: 800px;*/
  /*height: 800px;*/
  /*-webkit-box-sizing: border-box;*/
  /*-moz-box-sizing: border-box;*/
  /*box-sizing: border-box;*/
}
.game-container {
  margin: 60px auto;
  position: relative;
  padding: 15px;
  cursor: default;
  -webkit-touch-callout: none;
  -ms-touch-callout: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  -ms-touch-action: none;
  touch-action: none;
  background: #74c69d !important;
  border-radius: 6px;
  width: 800px;
  height: 800px;
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}
.game-container .game-message {
  /*display: none;*/
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background: rgba(116, 198, 157, 0.5);
  z-index: 100;
  text-align: center;
}
.game-container .game-message p {
  font-size: 60px;
  font-weight: bold;
  height: 60px;
  line-height: 60px;
  margin-top: 222px;
}
.game-container .game-message .lower {
  display: block;
  margin-top: 59px;
}
.game-container .game-message a {
  display: inline-block;
  background: #52b788;
  border-radius: 3px;
  padding: 0 20px;
  text-decoration: none;
  color: #f9f6f2;
  height: 40px;
  line-height: 42px;
  margin-left: 9px;
}
.game-container .game-message .keep-playing-button {
  display: none;
}
.game-container .game-message.game-won {
  background: rgba(64, 145, 108, 0.7);
  color: #f9f6f2;
}
.game-container .game-message.game-won .keep-playing-button {
  display: inline-block;
}
.game-container .game-message.game-won, .game-container .game-message.game-over {
  display: block;
}
.grid-container {
  position: absolute;
  z-index: 1;
}
.grid-row {
  margin-bottom: 15px;
}
.grid-row:last-child {
  margin-bottom: 0;
}
.grid-row:after {
  content: "";
  display: block;
  clear: both;
}
.grid-cell {
  width: 106.25px;
  height: 106.25px;
  margin-right: 15px;
  float: left;
  border-radius: 3px;
  background: rgba(238, 228, 218, 0.35);
}
.grid-cell:last-child {
  margin-right: 0;
}
.cell-foodGarbage {
  background-color: #6a994e !important;
  color: white !important;
  display: flex;
}
.cell-otherGarbage {
  background-color: #576B95 !important;
  color: white !important;
  display: flex;
}
.cell-recyclableGarbage{
  background-color: #1196FF !important;
  color: white !important;
  display: flex;
}
.cell-hazardousGarbage {
  background-color: #FA5151 !important;
  color: white !important;
  display: flex;
}
.tile-container {
  position: absolute;
  z-index: 2;
}
.tile, .tile .tile-inner {
  width: 107px;
  height: 107px;
  line-height: 107px;
  font-size: 16px;
  animation: appear 200ms ease-in-out;
}
.tile.tile-position-1-1 {
  -webkit-transform: translate(0px, 0px);
  -moz-transform: translate(0px, 0px);
  -ms-transform: translate(0px, 0px);
  transform: translate(0px, 0px);
}
.tile.tile-position-1-2 {
  -webkit-transform: translate(0px, 121px);
  -moz-transform: translate(0px, 121px);
  -ms-transform: translate(0px, 121px);
  transform: translate(0px, 121px);
}
.tile.tile-position-1-3 {
  -webkit-transform: translate(0px, 242px);
  -moz-transform: translate(0px, 242px);
  -ms-transform: translate(0px, 242px);
  transform: translate(0px, 242px);
}
.tile.tile-position-1-4 {
  -webkit-transform: translate(0px, 363px);
  -moz-transform: translate(0px, 363px);
  -ms-transform: translate(0px, 363px);
  transform: translate(0px, 363px);
}
.tile.tile-position-2-1 {
  -webkit-transform: translate(121px, 0px);
  -moz-transform: translate(121px, 0px);
  -ms-transform: translate(121px, 0px);
  transform: translate(121px, 0px);
}
.tile.tile-position-2-2 {
  -webkit-transform: translate(121px, 121px);
  -moz-transform: translate(121px, 121px);
  -ms-transform: translate(121px, 121px);
  transform: translate(121px, 121px);
}
.tile.tile-position-2-3 {
  -webkit-transform: translate(121px, 242px);
  -moz-transform: translate(121px, 242px);
  -ms-transform: translate(121px, 242px);
  transform: translate(121px, 242px);
}
.tile.tile-position-2-4 {
  -webkit-transform: translate(121px, 363px);
  -moz-transform: translate(121px, 363px);
  -ms-transform: translate(121px, 363px);
  transform: translate(121px, 363px);
}
.tile.tile-position-3-1 {
  -webkit-transform: translate(242px, 0px);
  -moz-transform: translate(242px, 0px);
  -ms-transform: translate(242px, 0px);
  transform: translate(242px, 0px);
}
.tile.tile-position-3-2 {
  -webkit-transform: translate(242px, 121px);
  -moz-transform: translate(242px, 121px);
  -ms-transform: translate(242px, 121px);
  transform: translate(242px, 121px);
}
.tile.tile-position-3-3 {
  -webkit-transform: translate(242px, 242px);
  -moz-transform: translate(242px, 242px);
  -ms-transform: translate(242px, 242px);
  transform: translate(242px, 242px);
}
.tile.tile-position-3-4 {
  -webkit-transform: translate(242px, 363px);
  -moz-transform: translate(242px, 363px);
  -ms-transform: translate(242px, 363px);
  transform: translate(242px, 363px);
}
.tile.tile-position-4-1 {
  -webkit-transform: translate(363px, 0px);
  -moz-transform: translate(363px, 0px);
  -ms-transform: translate(363px, 0px);
  transform: translate(363px, 0px);
}
.tile.tile-position-4-2 {
  -webkit-transform: translate(363px, 121px);
  -moz-transform: translate(363px, 121px);
  -ms-transform: translate(363px, 121px);
  transform: translate(363px, 121px);
}
.tile.tile-position-4-3 {
  -webkit-transform: translate(363px, 242px);
  -moz-transform: translate(363px, 242px);
  -ms-transform: translate(363px, 242px);
  transform: translate(363px, 242px);
}
.tile.tile-position-4-4 {
  -webkit-transform: translate(363px, 363px);
  -moz-transform: translate(363px, 363px);
  -ms-transform: translate(363px, 363px);
  transform: translate(363px, 363px);
}
.tile {
  position: absolute;
  -webkit-transition: 1000ms ease-in-out;
  -moz-transition: 1000ms ease-in-out;
  transition: 1000ms ease-in-out;
  -webkit-transition-property: -webkit-transform;
  -moz-transition-property: -moz-transform;
  transition-property: transform;
}
.tile .tile-inner {
  border-radius: 3px;
  background: #eee4da;
  text-align: center;
  font-weight: bold;
  z-index: 10;
  font-size: 55px;
  animation: appear 200ms ease-in-out;
}
.tile.tile-super .tile-inner {
  color: #f9f6f2;
  background: #3c3a32;
  font-size: 30px;
  animation: appear 200ms ease-in-out;
}

.tile-merged .tile-inner {
  z-index: 20;
  animation: appear 200ms ease-in-out;
}
.above-game:after {
  content: "";
  display: block;
  clear: both;
}
@media screen and (max-width:520px) {
  html, body {
    font-size: 15px;
  }
  body {
    margin: 20px 0;
    padding: 0 20px;
  }
  .restart-button-class {
    padding-right: 8%;
  }
  .footer-img {
    height: 160px;
  }
  .game-container {
    margin-top: 17px;
    position: relative;
    padding: 10px;
    cursor: default;
    -webkit-touch-callout: none;
    -ms-touch-callout: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    -ms-touch-action: none;
    touch-action: none;
    background: #95d5b2;
    border-radius: 6px;
    width: 315px;
    height: 315px;
    -webkit-box-sizing: border-box;
    -moz-box-sizing: border-box;
    box-sizing: border-box;
  }
  .game-container .game-message {
    display: none;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background: rgba(238, 228, 218, 0.8);
    z-index: 100;
    text-align: center;

  }
  .game-container .game-message .over-msg {
    display: block;
    font-size: 30px;
    font-weight: bold;
    height: 30px;
    line-height: 30px;
    /*margin-top: 222px;*/
    margin-top: 59px;
    color: #e63946;
  }

  .game-container .game-message .lower {
    display: block;
    margin-top: 59px;
    /*----------------------  */
    position: relative;
    left: 17%;
    border-radius: 10rpx;
    font-size: 25px;
    text-align: center;
    width: 60%;
    height: 400rpx;
  }
  .game-container .game-message .retry-button {
    display: inline-block;
    background: #bb3e03;
    border-radius: 10px;
    padding: 0 20px;
    text-decoration: none;
    color: #f9f6f2;
    height: 140rpx;
    line-height: 142rpx;
    margin-left: 19px;
    font-weight: bold;
  }
  .game-container .game-message .keep-playing-button {
    display: none;
  }
  .game-container .game-message.game-won {
    background: rgba(237, 194, 46, 0.5);
    color: #f9f6f2;
  }
  .game-container .game-message.game-won .keep-playing-button {
    display: inline-block;
  }
  .game-container .game-message.game-won, .game-container .game-message.game-over {
    display: block;
  }
  .grid-container {
    position: absolute;
    z-index: 1;
  }
  .grid-row {
    margin-bottom: 5px;
  }
  .grid-row:last-child {
    margin-bottom: 0;
  }
  .grid-row:after {
    content: "";
    display: block;
    clear: both;
  }
  .grid-cell {
    width: 70px;
    height: 70px;
    margin-right: 5px;
    float: left;
    border-radius: 3px;
    background: rgba(238, 228, 218, 0.35);
  }
  .grid-cell:last-child {
    margin-right: 0;
  }

  .tile, .tile .tile-inner {
    width: 70.5px;
    height: 70.5px;
    line-height: 62.5px;
    animation: appear 200ms ease-in-out;
  }
  .tile.tile-position-1-1 {
    -webkit-transform: translate(0px, 0px);
    -moz-transform: translate(0px, 0px);
    -ms-transform: translate(0px, 0px);
    transform: translate(0px, 0px);
  }
  .tile.tile-position-1-2 {
    -webkit-transform: translate(0px, 67px);
    -moz-transform: translate(0px, 67px);
    -ms-transform: translate(0px, 67px);
    transform: translate(0px, 67px);
  }
  .tile.tile-position-1-3 {
    -webkit-transform: translate(0px, 135px);
    -moz-transform: translate(0px, 135px);
    -ms-transform: translate(0px, 135px);
    transform: translate(0px, 135px);
  }
  .tile.tile-position-1-4 {
    -webkit-transform: translate(0px, 202px);
    -moz-transform: translate(0px, 202px);
    -ms-transform: translate(0px, 202px);
    transform: translate(0px, 202px);
  }
  .tile.tile-position-2-1 {
    -webkit-transform: translate(67px, 0px);
    -moz-transform: translate(67px, 0px);
    -ms-transform: translate(67px, 0px);
    transform: translate(67px, 0px);
  }
  .tile.tile-position-2-2 {
    -webkit-transform: translate(67px, 67px);
    -moz-transform: translate(67px, 67px);
    -ms-transform: translate(67px, 67px);
    transform: translate(67px, 67px);
  }
  .tile.tile-position-2-3 {
    -webkit-transform: translate(67px, 135px);
    -moz-transform: translate(67px, 135px);
    -ms-transform: translate(67px, 135px);
    transform: translate(67px, 135px);
  }
  .tile.tile-position-2-4 {
    -webkit-transform: translate(67px, 202px);
    -moz-transform: translate(67px, 202px);
    -ms-transform: translate(67px, 202px);
    transform: translate(67px, 202px);
  }
  .tile.tile-position-3-1 {
    -webkit-transform: translate(135px, 0px);
    -moz-transform: translate(135px, 0px);
    -ms-transform: translate(135px, 0px);
    transform: translate(135px, 0px);
  }
  .tile.tile-position-3-2 {
    -webkit-transform: translate(135px, 67px);
    -moz-transform: translate(135px, 67px);
    -ms-transform: translate(135px, 67px);
    transform: translate(135px, 67px);
  }
  .tile.tile-position-3-3 {
    -webkit-transform: translate(135px, 135px);
    -moz-transform: translate(135px, 135px);
    -ms-transform: translate(135px, 135px);
    transform: translate(135px, 135px);
  }
  .tile.tile-position-3-4 {
    -webkit-transform: translate(135px, 202px);
    -moz-transform: translate(135px, 202px);
    -ms-transform: translate(135px, 202px);
    transform: translate(135px, 202px);
  }
  .tile.tile-position-4-1 {
    -webkit-transform: translate(202px, 0px);
    -moz-transform: translate(202px, 0px);
    -ms-transform: translate(202px, 0px);
    transform: translate(202px, 0px);
  }
  .tile.tile-position-4-2 {
    -webkit-transform: translate(202px, 67px);
    -moz-transform: translate(202px, 67px);
    -ms-transform: translate(202px, 67px);
    transform: translate(202px, 67px);
  }
  .tile.tile-position-4-3 {
    -webkit-transform: translate(202px, 135px);
    -moz-transform: translate(202px, 135px);
    -ms-transform: translate(202px, 135px);
    transform: translate(202px, 135px);
  }
  .tile.tile-position-4-4 {
    -webkit-transform: translate(202px, 202px);
    -moz-transform: translate(202px, 202px);
    -ms-transform: translate(202px, 202px);
    transform: translate(202px, 202px);
  }
  .tile .tile-inner {
    font-size: 16px;
    line-height: 20px !important;
    margin: auto;
    animation: appear 200ms ease-in-out;
  }
  .game-message p {
    font-size: 30px !important;
    height: 30px !important;
    line-height: 30px !important;
    margin-top: 90px !important;
  }
  .game-message .lower {
    margin-top: 30px !important;
  }
}

@keyframes appear {
  0% {
    opacity: 0;
    transform: scale(0);
  }
  10% {
    opacity: 0.1;
    transform: scale(0.5);
  }
  20% {
    opacity: 0.2;
    transform: scale(0.55);
  }
  30% {
    opacity: 0.3;
    transform: scale(0.6);
  }
  40% {
    opacity: 0.4;
    transform: scale(0.65);
  }
  50% {
    opacity: 0.5;
    transform: scale(0.70);
  }
  60% {
    opacity: 0.6;
    transform: scale(0.75);
  }
  70% {
    opacity: 0.7;
    transform: scale(0.80);
  }
  80% {
    opacity: 0.8;
    transform: scale(0.85);
  }
  90% {
    opacity: 0.9;
    transform: scale(0.90);
  }
  100% {
    opacity: 1;
    transform: scale(1);
  }
}
</style>
